﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NENAData.EF;
using NENAData.Factory;
using System.Data.SqlClient;

namespace NENAData.Events
{
    public class SessionRegistration
    {
        private static NENA_MembershipEntities GetConnection()
        {
            var x = new SQLMembership();
            return x.DBConnection();
        }

        private static string GetSQLString()
        {
            var x = new SQLMembership();
            return x.GetMembershipDBString();
        }

        public void Add(EF.EventSessionRegistration oEventSessionRegistration)
        {

            NENA_MembershipEntities db = GetConnection();

            db.AddToEventSessionRegistrations(oEventSessionRegistration);
            db.SaveChanges();
        }



        public List<EF.EventSessionRegistration> GetWithSlotSession(int intEventRegistrationID)
        {
            NENA_MembershipEntities db = GetConnection();

            //http://stackoverflow.com/questions/315966/how-do-you-construct-a-linq-to-entities-query-to-load-child-objects-directly-ins
            List<EF.EventSessionRegistration> oEventSessionRegistration = 
                (from EventSessionRegistration esr in
                                                db.EventSessionRegistrations.Include("SlotSession")
                                                where esr.EventRegistration.EventRegistrationID == intEventRegistrationID
                                                       select esr).ToList();
            
            return oEventSessionRegistration;

        }

        public void AddbySQL(int intSessionId, int intEventTransactionId, string strComment)
        {

            string strSQL = @"insert into EventSessionRegistration 
                (SessionId, EventRegistrationId, Comments) 
                values (@SessionId, @EventRegistrationId, @Comments)";

            SqlConnection objSqlCon = new SqlConnection(GetSQLString());
            SqlCommand cmd = new SqlCommand(strSQL, objSqlCon);
            cmd.Parameters.AddWithValue("@SessionId", intSessionId);
            cmd.Parameters.AddWithValue("@EventRegistrationId", intEventTransactionId);
            cmd.Parameters.AddWithValue("@Comments", strComment);
            objSqlCon.Open();
            cmd.ExecuteNonQuery();
            objSqlCon.Close();

        }


    }
}
